﻿using Cyl.Communication;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;

namespace VisionApp.PreProcess.Net8;

public interface IPreProcessService
{
    Dictionary<string ,object> GetParameters(ICommDevice commDevice);
}
public class PreProcessService : IPreProcessService
{
    private readonly ILogger<PreProcessService> logger;
    private readonly IMemoryCache cache;

    private ICommDevice? commDevice;
    public PreProcessService(ILogger<PreProcessService> logger, IMemoryCache cache)
    {
        this.logger = logger;
        this.cache = cache;
    }
    public Dictionary<string, object> GetParameters(ICommDevice commDevice)
    {
        /*
         * 两种方式给插件传递参数
         * 1、通过返回的字典
         * 2、通过缓存
         */
        // 缓存读写的例子
        var newSN = "123456789";
        // 写入缓存
        cache.Set("SN", newSN);
        // 读取缓存
        if(cache.TryGetValue("SN",out string sn))
        {
            // key 存在
            logger.LogInformation(sn);
        }
        else
        {
            // key 不存在，可以做相应处理
        }

        logger.LogWarning("没有执行获取数据的过程");

        var reslut= new  Dictionary<string, object>();
        reslut.Add("SN", "123456789");
        reslut.Add("para1", "10");
        return reslut;
    }
}